<template>
  <Modal
    :options="getModalOptions"
    :on-positive-click="handleConfirm"
    :on-negative-click="handleClose"
    :on-close="handleClose"
    style="border: 1px solid rgb(49, 69, 114);"
  >
    <template #Context>
      <n-form
        ref="formRef"
        :model="userForm"
        :rules="userRules"
        :label-width="100"
        label-placement="left"
        require-mark-placement="left"
        :disabled="disabled"
      >
        <div class="flex__body">
          <template v-for="(item,index) in getFormOptions" :key="index">
            <n-form-item :label="item.label" :path="item.key" :class="{ 'flex__item': !['oss','editor'].includes(item.type) }">
              <n-input v-if="item.type === 'input'" v-model:value="userForm[item.key]" v-bind="item.props" />
              
              <n-radio-group v-if="item.type === 'radio'" v-model:value="userForm[item.key]" :name="item.key">
              <!-- {{userForm[item.key]}} -->
                <n-space>
                  <n-radio v-for="(cItem,cIndex) in item.options" :key="`${item.key}_${cIndex}`" :value="cItem.value"> {{ cItem.label }}</n-radio> 
                </n-space>
              </n-radio-group>
            </n-form-item>
          </template>
        </div>

      </n-form>
    </template>
  </Modal>
</template>

<script>
import { form } from '../tools/form.js'
import Modal from '@/components/Modal/index.vue'
import { roleCreate, roleUpdate } from '@/api/system/role.js'
import { defineComponent, ref, reactive, computed, toRefs } from 'vue'
import { debounce } from '@/utils/index.js'

export default defineComponent({
  name: 'UserModal',
  components: { Modal },
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    type: {
      type: String,
      default: 'create'
    },
    data: {
      type: Object,
      default: () => {}
    }
  },
  emits: {
    'update:visible': null,
    'reload': null
  },
  setup(props, { emit }) {
    const MODAL_TYPE = {
      'create': '新增角色',
      'preview': '角色详情',
      'update': '编辑角色'
    }
    const { userForm, userRules } = form
    const formRef = ref()
    const data = reactive({
      userForm: {
        ...userForm,
        ...props.data
      },
      userRules: {
        ...userRules
      },
      disabled: props.type === 'preview'
    })
    const getModalOptions = computed(() => {
      return {
        title: MODAL_TYPE[props.type],
        width: 600,
        show: props.visible,
        trapFocus: false,
        negativeText: '取消',
        positiveText: '确认'
      }
    })

    const getFormOptions = computed(() => {
      return {
        ...form.formItem
      }
    })

    const handleConfirm = debounce(() => {
      formRef.value.validate((errors) => {
        if (!errors) {
          const params = { ...data.userForm }
          if (props.type === 'preview') {
            emit('reload')
            handleClose()
          } else if (params.id) {
            roleUpdate(params).then(res => {
              if (res.code === 0) {
                emit('reload')
                handleClose()
              }
            }).catch(e => {
              console.log(e)
            })
          } else {
            roleCreate(params).then(res => {
              if (res.code === 0) {
                emit('reload')
                handleClose()
              }
            })
          }
        }
      })
    }, 2000, true)

    /* 关闭弹窗 */
    const handleClose = () => {
      emit('update:visible', false)
    }

    return {
      ...toRefs(data),
      formRef,
      getModalOptions,
      getFormOptions,
      handleConfirm,
      handleClose
    }
  }
})
</script>

<style scoped lang='scss'>
.n-input-number{
  width: 100%;
}

.flex__body{
  display: flex;
  width: 100%;
  flex-wrap: wrap;
  .n-form-item{
    width: 100%;
  }
  // .flex__item{
  //   width: 50%;
  // }
}

.n-date-picker{
  width: 100%;
}
</style>
